package com.mall.manager.service;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.mall.common.dao.MongoDBManager;
import com.mall.common.dao.MysqlDBManager;
import com.mall.untils.DateUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
/**
 * @按钮点击统计
 * @author liugb
 * @2016 7 13
 */
@Service
public class SdkClickService {
	//日志信息
	private Logger logger = LoggerFactory.getLogger(SdkClickService.class);
	@Resource
    private JdbcTemplate jdbcTemplate;
	/**
	 * 按钮点击统计
	 * @return
	 */
	public List<String> findsdkClick() {
//		List<DBObject> findAll = MongoDBManager.findAll("sdk_collection");		
		//当前日期的前一天
		String beforDate = DateUtils.getNowBeforDay();
		//按时间检索前一天的数据
		BasicDBObject dbo = new BasicDBObject();
		dbo.append("commit_time", new BasicDBObject("$gte", beforDate+" 00:00:00"));
		dbo.append("commit_time", new BasicDBObject("$lte", beforDate+" 23:59:59"));
		BasicDBList list = new BasicDBList();
		list.add(dbo);
		BasicDBObject d = new BasicDBObject();
		d.put("$and", list);		
		List<DBObject> findAll = MongoDBManager.findForCommitTime("sdk_collection", d);
		List<String> stingList = new ArrayList<String>();
		String res3;
		try {
			for (DBObject dbObject : findAll) {			
				String string = String.valueOf(dbObject.get("click_time"));
				String string1 = String.valueOf(dbObject.get("click_count"));
				String string2 = String.valueOf(dbObject.get("data_type"));
				if(!string.equals("null")){				
				   res3 = ""+string+","+string1+","+string2;
				   stingList.add(res3);
				}
			}	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.info(e.getMessage());
		}
	
		return stingList;
	}	
	/**
	 * 按钮点击统计
	 * @param type
	 * @param sum
	 * @throws SQLException 
	 */
	public static void manager_insert_sdkclick(String type,Integer sum){
		Connection conn = null;
		PreparedStatement ps = null;
		try {			
			String sql="insert into t_sdk_click(click_count,data_type,create_time,update_time) values(?,?,?,?)";
			conn = MysqlDBManager.getConnection();
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, sum);
			ps.setString(2, type);
			ps.setTimestamp(3, DateUtils.getNowBeforTimestamp());
			ps.setTimestamp(4, DateUtils.getTimestamp());
			ps.addBatch();
			ps.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}	
	/**
     * @点击类型添加数据
     * @param paraMap
     * @return
     */
	public int manager_insert_sdkclick(Map<String, Object> paraMap) {
		String click_count = MapUtils.getString(paraMap, "click_count");
		String data_type = MapUtils.getString(paraMap, "data_type");
		try {
			String sql="insert into t_sdk_click(id,click_count,data_type,create_time,udpate_time) values(?,?,?,now(),now())";
			int update = jdbcTemplate.update(sql,click_count,data_type);
			return update;
		} catch (Exception e) {
			logger.debug(e.getMessage());
			return 0;
		}
	}
}
